package model.bo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import model.bean.Calendar;
import model.bean.ListCalendar;
import model.dao.CalendarDAO;

public class CalendarBO {

	CalendarDAO calendarDAO = new CalendarDAO();

	public ArrayList<ListCalendar> getCalendar() {

		ArrayList<ListCalendar> arrCalendar = calendarDAO.getCalendar();
		for (ListCalendar c : arrCalendar) {
			c.setDate(day_of_week(c.getDate()));
			ArrayList<Calendar> calendarArr = c.getCalendarArr();
			for (Calendar cl : calendarArr) {
				String title = "- " + time_of_day(cl.getStarttime()) + "-"
						+ time_of_day(cl.getEndtime()) + ": " + cl.getTitle()
						+ " - " + cl.getContent();

				// do dai thong tin dua ra
				if (title.length() > 100) {
					cl.setTitle(title.substring(0, 100));
				} else {
					cl.setTitle(title);
				}
			}
		}
		return arrCalendar;

	}

	public String day_of_week(String input_date) {

		String dayOfWeek = null;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		java.util.Date date;
		try {
			date = format.parse(input_date);
			java.util.Calendar c = java.util.Calendar.getInstance();
			c.setTime(date);
			String[] dayOfWeekList = { "", "Chủ Nhật", "Thứ Hai", "Thứ Ba",
					"Thứ Tư", "Thứ Năm", "Thứ Sáu", "Thứ Bảy" };

			dayOfWeek = dayOfWeekList[c.get(java.util.Calendar.DAY_OF_WEEK)]
					+ ": " + new SimpleDateFormat("dd/MM/yyyy").format(date);

			dayOfWeek = dayOfWeekList[c.get(java.util.Calendar.DAY_OF_WEEK)]
					+ ": " + new SimpleDateFormat("dd/MM/yyyy").format(date);

		} catch (ParseException e) {
			e.printStackTrace();
		}
		return dayOfWeek;
	}

	public ArrayList<Calendar> viewCalendar() {

		ArrayList<Calendar> calendar = calendarDAO.viewCalendar();
		for (Calendar c : calendar) {
			c.setDate(day_of_week(c.getDate()));
			String title = "- " + time_of_day(c.getStarttime()) + "-"
					+ time_of_day(c.getEndtime()) + ": " + c.getTitle() + " - "
					+ c.getContent();

			// do dai thong tin dua ra
			if (title.length() > 100) {
				c.setContent(title.substring(0, 100));
			} else {
				c.setContent(title);
			}
		}
		return calendar;
	}

	// void time_of_date
	public String time_of_day(String input_time) {

		String[] time = input_time.split(":");
		if ("00".equalsIgnoreCase(time[1])) {
			input_time = input_time.split(":")[0] + "h";
		} else {
			input_time = input_time.split(":")[0] + "h"
					+ input_time.split(":")[1];
		}

		return input_time;

	}

	public ArrayList<Calendar> viewCalendar(String time) {

		ArrayList<Calendar> calendar = calendarDAO.viewCalendar(time);
		for (Calendar c : calendar) {
			c.setDate(day_of_week(c.getDate()));
			String title = "- " + time_of_day(c.getStarttime()) + "-"
					+ time_of_day(c.getEndtime()) + ": " + c.getTitle() + " - "
					+ c.getContent();

			// do dai thong tin dua ra
			if (title.length() > 100) {
				c.setContent(title.substring(0, 100));
			} else {
				c.setContent(title);
			}
		}
		return calendar;
	}

	// insert calendar
	public boolean insertCalendar(String date, String starttime,
			String endtime, String title, String content, String status,
			String location) {

		/**
		 * kiem tra thoi gian co hop le hay khong
		 *
		 */
		if (calendarDAO.checkTimeOfDay(date, starttime, endtime)) {
			int id = calendarDAO.getCalendarID();
			return calendarDAO.insertCalendar(id, date, starttime, endtime,
					title, content, status, location);
		} else {
			return false;
		}

	}

	/**
	 * Hien thi lich quan trong trong tuan
	 * 
	 * @return ArrayList<ListCalendar>
	 * 
	 *         TriLD
	 */
	public ArrayList<Calendar> getCalendarStatus() {
		return calendarDAO.getCalendarStatus();

	}

	public boolean updateCalendar(String idCalendar, String date,
			String starttime, String endtime, String title, String content,
			String status, String location) {
			return calendarDAO.updateCalendar(idCalendar, date, starttime, endtime, title, content, status, location);
	}

}
